/**
 * class which represent the Mathematic Expression A-B
 * When A and B are themselfes Mathematical Expressions
 * @author Nadav Cohen, Matan Monitz
 *
 */
public class Subtraction implements Expression {
	public Expression left = null;		//A
	public Expression right = null;		//B
	
	/**
	 * 	Constructor Method -
	 * create a new Subtraction object with exp1 parameter as the left expression (left), and exp2 parameter as the right expression (right)
	 * copy pointers of Expressions exp1 & exp2, doesnt create new Expressions.
	 * incase one of given parameters is not initialized - points to null - method throw runtime exception
	 * @param exp1 - an object which implements the Expression interface, must not be null
	 * @param exp2 - an object whice implements the Expression interface, must not be null
	 */	
	public Subtraction(Expression exp1,Expression exp2)
	{
		if(exp1==null || exp2==null)
			throw new RuntimeException("Substraction couldn't be constracted - Variable given uninitialzed!");
		left=exp1;
		right=exp2;		
	}
	
	public String toString()
	{
		return "(" + left.toString() + "-" + right.toString() + ")";
	}
	
	public double evaluate(Assignments assignments) {
		if(assignments==null)   // incase parameter given points to nowhere, throw exception
			throw new RuntimeException("Subtraction evalutation didnt succeed - Assignments given uninitialzed!");
		return left.evaluate(assignments)-right.evaluate(assignments);
	}
	public Expression derivative(Variable var) {
		if(var==null) 	// incase parameter given points to nowhere, throw exception (doesnt know by which variable to derive)
			throw new RuntimeException("Subtraction Derivative didnt succeed - Variable given uninitialzed!");
		return new Subtraction(left.derivative(var),right.derivative(var));
	}

/**
* compare parameter Object other to this Subtraction. Objects Equals if:
* - parameter other is type Subtraction and not null
* - parameter other left Expression is the same left Expression as this object. (left expressions are the same)
* - parameter other right Expression is the same right Expression as this object. (right expressions are the same)
*  @return true if equals, else return false.
*/		
	public boolean equals(Object other)
	{
			if(other instanceof Subtraction)
				return (this.right.equals(((Subtraction)other).right)) && (this.left.equals(((Subtraction)other).left));
		return false;
	}

}
